using EasyNetQ;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MF.Bus
{
    /// <summary>
    /// 总线接口实现类
    /// </summary>
    public class MFBusDefault : IMFBus
    {

        private readonly ILogger<MFBusDefault> _logger;

        private readonly IBus _bus;

        public MFBusDefault(IBus bus, ILogger<MFBusDefault> logger)
        {
            _bus = bus;
            _logger = logger;
        }

        /// <summary>
        /// 发布指定事件的消息(底层实现的异步)
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventName"></param>
        public void PublishAsync(BusMessage message, string eventName)
        {
            try
            {
                _bus.PubSub.PublishAsync(message, eventName);
                _logger.LogInformation(string.Format("事件{0}的消息已发送,消息体:{1}", eventName, message));
            }
            catch(Exception ex)
            {
                _logger.LogError(ex,string.Format("事件{0}的消息发送失败,消息体:{1}", eventName, message));
            }
        }
    }
}
